/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserInterface.java
 *
 * Created on 5-apr-2010, 18.51.12
 */
package meshindexer;

import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import meshindexer.UMLSinterface.Client;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.plaf.basic.BasicListUI.ListDataHandler;
import meshindexer.XMLParser.MedlineParser;
import meshindexer.idretriever.IdRetriever;

/**
 *
 * @author Dallaz
 */
public class UserInterface extends javax.swing.JFrame {

    /** Creates new form UserInterface */
    public UserInterface() {
        initComponents();

        this.setResizable(false);
        jListMesh.setModel(new DefaultListModel());
        jListSearch.setModel(new DefaultListModel());
        jTerm.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                if (jTerm.getText().length() > 0) {
                    jButtonAdd.setEnabled(true);
                } else {
                    jButtonAdd.setEnabled(false);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                if (jTerm.getText().length() > 0) {
                    jButtonAdd.setEnabled(true);
                } else {
                    jButtonAdd.setEnabled(false);
                }
            }

            public void changedUpdate(DocumentEvent e) {
                if (jTerm.getText().length() > 0) {
                    jButtonAdd.setEnabled(true);
                } else {
                    jButtonAdd.setEnabled(false);
                }
            }
        });
    }
    private Client client = new Client();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButtonSearch = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListMesh = new javax.swing.JList();
        jTextUser = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jVersion = new javax.swing.JComboBox();
        jButtonRemove = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListSearch = new javax.swing.JList();
        jTerm = new javax.swing.JTextField();
        jButtonAdd = new javax.swing.JButton();
        progress = new javax.swing.JProgressBar();
        jTextPw = new javax.swing.JPasswordField();
        jScrollPane3 = new javax.swing.JScrollPane();
        console = new javax.swing.JTextPane();
        jComboArt = new javax.swing.JComboBox();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAbs = new javax.swing.JTextArea();
        webButton = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        title = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        clear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Username:");
        jLabel1.setName("jLabel1"); // NOI18N

        jButtonSearch.setText("research");
        jButtonSearch.setEnabled(false);
        jButtonSearch.setName("jButtonSearch"); // NOI18N
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jLabel5.setText("Equivalent MeSH terms");
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jListMesh.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListMesh.setEnabled(false);
        jListMesh.setName("jListMesh"); // NOI18N
        jListMesh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListMeshMouseClicked(evt);
            }
        });
        jListMesh.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListMeshValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jListMesh);

        jTextUser.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextUser.setText("elettrosmog1");
        jTextUser.setName("jTextUser"); // NOI18N

        jLabel3.setText("UMLS Version:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Term to Search:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel2.setText("Password:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel6.setText("Abstract of Medline Article:");
        jLabel6.setName("jLabel6"); // NOI18N

        jVersion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2009AB" }));
        jVersion.setName("jVersion"); // NOI18N

        jButtonRemove.setText("remove");
        jButtonRemove.setEnabled(false);
        jButtonRemove.setName("jButtonRemove"); // NOI18N
        jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListSearch.setName("jListSearch"); // NOI18N
        jListSearch.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListSearchValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListSearch);

        jTerm.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTerm.setName("jTerm"); // NOI18N

        jButtonAdd.setText("add to Terms");
        jButtonAdd.setEnabled(false);
        jButtonAdd.setName("jButtonAdd"); // NOI18N
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        progress.setName("progress"); // NOI18N
        progress.setStringPainted(true);

        jTextPw.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextPw.setText("umls2010");
        jTextPw.setName("jTextPw"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        console.setEditable(false);
        console.setName("console"); // NOI18N
        jScrollPane3.setViewportView(console);

        jComboArt.setEnabled(false);
        jComboArt.setName("jComboArt"); // NOI18N
        jComboArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboArtActionPerformed(evt);
            }
        });

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTextAbs.setColumns(20);
        jTextAbs.setEditable(false);
        jTextAbs.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jTextAbs.setLineWrap(true);
        jTextAbs.setRows(5);
        jTextAbs.setWrapStyleWord(true);
        jTextAbs.setAutoscrolls(false);
        jTextAbs.setEnabled(false);
        jTextAbs.setName("jTextAbs"); // NOI18N
        jScrollPane4.setViewportView(jTextAbs);

        webButton.setText("go to the Article");
        webButton.setEnabled(false);
        webButton.setName("webButton"); // NOI18N
        webButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                webButtonActionPerformed(evt);
            }
        });

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        title.setColumns(20);
        title.setEditable(false);
        title.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        title.setLineWrap(true);
        title.setRows(2);
        title.setWrapStyleWord(true);
        title.setEnabled(false);
        title.setName("title"); // NOI18N
        jScrollPane5.setViewportView(title);

        jButton1.setText("get Articles");
        jButton1.setEnabled(false);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Search for Exact Match");
        jCheckBox1.setEnabled(false);
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        clear.setText("clear");
        clear.setEnabled(false);
        clear.setName("clear"); // NOI18N
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                            .addComponent(jButtonAdd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jButtonRemove))
                                .addGap(84, 84, 84)
                                .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(clear, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCheckBox1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTerm, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                                    .addComponent(jTextUser, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                                    .addComponent(jTextPw, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                                    .addComponent(jVersion, 0, 234, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboArt, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(webButton, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jTextUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(webButton)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonAdd))
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonRemove)
                            .addComponent(jButtonSearch))
                        .addGap(19, 19, 19)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jCheckBox1)
                            .addComponent(clear)))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        client.setPassword(new String(jTextPw.getPassword()));
        client.setUMLSVersion((String) jVersion.getSelectedItem());
        client.setUsername(jTextUser.getText());
        progress.setMaximum(jListSearch.getModel().getSize());
        progress.setMinimum(0);
        DefaultListModel dl = (DefaultListModel) jListMesh.getModel();
        for (int i = 0; i < jListSearch.getModel().getSize(); i++) {
            console.setText("searching for the term \"" + (String) jListSearch.getModel().getElementAt(i) + "\"...");
            console.update(console.getGraphics());
            String[] meshTerms = null;
            meshTerms = client.getMeshTerms((String) jListSearch.getModel().getElementAt(i));
            if (meshTerms.length > 0) {
                for (String n : meshTerms) {
                    if (!dl.contains(n)) {
                        dl.addElement(n);
                    }
                }
                jListMesh.setEnabled(true);
                jButton1.setEnabled(true);
                jCheckBox1.setEnabled(true);
                clear.setEnabled(true);
            }
            progress.setValue(i + 1);
            progress.update(progress.getGraphics());
        }
        jButtonRemove.setEnabled(false);
        jButtonSearch.setEnabled(false);
        DefaultListModel sm = (DefaultListModel) jListSearch.getModel();
        sm.clear();
        console.setText("");
}//GEN-LAST:event_jButtonSearchActionPerformed

    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
        DefaultListModel model = (DefaultListModel) jListSearch.getModel();
        model.remove(jListSearch.getSelectedIndex());
        Vector<String> temp = new Vector<String>();
        if (!model.isEmpty()) {
            jButtonSearch.setEnabled(true);
        } else {
            jButtonSearch.setEnabled(false);
        }
}//GEN-LAST:event_jButtonRemoveActionPerformed

    private void jListSearchValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListSearchValueChanged
        if (jListSearch.getSelectedIndex() > -1) {
            jButtonRemove.setEnabled(true);
        }
        else
            jButtonRemove.setEnabled(false);
        if (jListSearch.getModel().getSize()>0)
            jButtonSearch.setEnabled(true);
        else
            jButtonSearch.setEnabled(false);
}//GEN-LAST:event_jListSearchValueChanged

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        DefaultListModel model = (DefaultListModel) jListSearch.getModel();
        model.addElement(jTerm.getText());
        jTerm.setText("");
        jButtonAdd.setEnabled(false);
        jButtonSearch.setEnabled(true);
}//GEN-LAST:event_jButtonAddActionPerformed

    private void jListMeshValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListMeshValueChanged
    }//GEN-LAST:event_jListMeshValueChanged

    private void jListMeshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListMeshMouseClicked
        // to implement
    }//GEN-LAST:event_jListMeshMouseClicked

    private void jComboArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboArtActionPerformed

        try {
            String id = jComboArt.getModel().getSelectedItem().toString();
            MedlineParser mp = new MedlineParser("http://www.ncbi.nlm.nih.gov/pubmed/" + id + "?dopt=xml");
            String abs = mp.getAbstract();
            jTextAbs.setText(abs);
            jTextAbs.setCaretPosition(0);
            title.setText(mp.getTitle());
            title.setCaretPosition(0);
            jTextAbs.update(jTextAbs.getGraphics());
        } catch (Exception e) {
            jTextAbs.setText("");
            title.setText("");
        }
    }//GEN-LAST:event_jComboArtActionPerformed

    private void webButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_webButtonActionPerformed
        if (jComboArt.getSelectedIndex() >= 0) {
            String id = jComboArt.getModel().getSelectedItem().toString();
            String url = "http://www.ncbi.nlm.nih.gov/pubmed/" + id;
            try {
                java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
            } catch (IOException ex) {
            }
        } else {
            console.setText("No article selected");
        }
    }//GEN-LAST:event_webButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        console.setText("Research in progress...");
        console.update(console.getGraphics());
        if (jListMesh.getSelectedIndices().length > 0) {
            try {
                String term = "";
                for (int i = 0; i < jListMesh.getSelectedIndices().length; i++) {
                    if (jCheckBox1.isSelected()) {
                        term += " \"" + jListMesh.getModel().getElementAt(jListMesh.getSelectedIndices()[i]).toString() + "\"" + "[mh]";
                    } else {
                        term += " " + jListMesh.getModel().getElementAt(jListMesh.getSelectedIndices()[i]).toString() + "[mh]";
                    }
                    if (i != jListMesh.getSelectedIndices().length - 1) {
                        term += " AND ";
                    }
                }
                Vector<String> articles_id = new IdRetriever(term).getIds();
                DefaultComboBoxModel p = (DefaultComboBoxModel) jComboArt.getModel();
                p.removeAllElements();
                progress.setMaximum(articles_id.size());
                progress.setMinimum(0);
                int count = 0;
                for (String n : articles_id) {
                    p.addElement(n);
                    progress.setValue(count++);
                    if (count%10==0)
                        progress.update(progress.getGraphics());
                }
                if (articles_id.size() > 200) {
                    console.setText("Warning! The research has produced " + articles_id.size() + " articles. Consider refining the research.");
                } else {
                    console.setText("");
                }
                jComboArt.setEnabled(true);
                webButton.setEnabled(true);
                title.setEnabled(true);
                jTextAbs.setEnabled(true);
                jTextAbs.setCaretPosition(0);
                title.setCaretPosition(0);
                jComboArt.update(jComboArt.getGraphics());
            } catch (Exception ex) {
                console.setText("The research found no articles");
                DefaultComboBoxModel p = (DefaultComboBoxModel) jComboArt.getModel();
                p.removeAllElements();
                jComboArt.setEnabled(false);
                webButton.setEnabled(false);
                title.setEnabled(false);
                jTextAbs.setEnabled(false);
                jComboArt.update(jComboArt.getGraphics());
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
        ((DefaultListModel)jListMesh.getModel()).clear();
        jListMesh.setEnabled(false);
                jButton1.setEnabled(false);
                jCheckBox1.setEnabled(false);
                clear.setEnabled(false);
    }//GEN-LAST:event_clearActionPerformed

    private class ItemsList extends AbstractListModel {

        private Vector<String> items;

        public ItemsList(Vector<String> temp) {
            items = temp;
        }

        public int getSize() {
            return items.size();
        }

        public Object getElementAt(int i) {
            return items.get(i);
        }
    };

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UserInterface().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clear;
    private javax.swing.JTextPane console;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboArt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jListMesh;
    private javax.swing.JList jListSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTextField jTerm;
    private javax.swing.JTextArea jTextAbs;
    private javax.swing.JPasswordField jTextPw;
    private javax.swing.JTextField jTextUser;
    private javax.swing.JComboBox jVersion;
    private javax.swing.JProgressBar progress;
    private javax.swing.JTextArea title;
    private javax.swing.JButton webButton;
    // End of variables declaration//GEN-END:variables
}
